<!doctype html>
<html lang="zh-CN" data-theme="light">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width,initial-scale=1" />
    <meta name="generator" content="VuePress 2.0.0-beta.68" />
    <meta name="theme" content="VuePress Theme Hope 2.0.0-beta.250" />
    <style>
      html {
        background: #fff;
      }

      html[data-theme="dark"] {
        background: #1d1e1f;
      }

      body {
        background: var(--bg-color);
      }
    </style>
    <script>
      const userMode = localStorage.getItem("vuepress-theme-hope-scheme");
      const systemDarkMode =
        window.matchMedia &&
        window.matchMedia("(prefers-color-scheme: dark)").matches;

      if (userMode === "dark" || (userMode !== "light" && systemDarkMode)) {
        document.documentElement.setAttribute("data-theme", "dark");
      }
    </script>
    <meta name="referrer" content="no-referrer"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.jpg"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png" type="image/png" sizes="512x512"><link rel="icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png" type="image/png" sizes="192x192"><link rel="manifest" href="/manifest.webmanifest" crossorigin="use-credentials"><meta name="theme-color" content="#46bd87"><link rel="apple-touch-icon" href="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png"><meta name="apple-mobile-web-app-capable" content="yes"><meta name="apple-mobile-web-app-status-bar-style" content="black"><meta name="msapplication-TileImage" content="https://gitee.com/eddie-lucas/images/raw/master/img/logo.png"><meta name="msapplication-TileColor" content="#ffffff"><meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, viewport-fit=cover"><title>NameServer | 小刘Learning</title><meta name="description" content="">
    <link rel="preload" href="/assets/style-3ede7958.css" as="style"><link rel="stylesheet" href="/assets/style-3ede7958.css">
    <link rel="modulepreload" href="/assets/app-d83e1369.js"><link rel="modulepreload" href="/assets/NameServer.html-6a201ec6.js"><link rel="modulepreload" href="/assets/NameServer.html-6ada07a1.js"><link rel="modulepreload" href="/assets/plugin-vue_export-helper-c27b6911.js">
    <link rel="prefetch" href="/assets/aboutMe.html-72f014c7.js" as="script"><link rel="prefetch" href="/assets/index.html-69cd5f22.js" as="script"><link rel="prefetch" href="/assets/Java集合.html-f6d1e995.js" as="script"><link rel="prefetch" href="/assets/MyBatis常见问题.html-3782c8f7.js" as="script"><link rel="prefetch" href="/assets/InnoDB的Buffer Pool.html-f0ae2755.js" as="script"><link rel="prefetch" href="/assets/InnoDB的行格式和页结构.html-31890643.js" as="script"><link rel="prefetch" href="/assets/MySQL中的各种锁.html-b819061d.js" as="script"><link rel="prefetch" href="/assets/MySQL中的排序.html-5442bdf2.js" as="script"><link rel="prefetch" href="/assets/MySQL事务与MVCC.html-11dea9ac.js" as="script"><link rel="prefetch" href="/assets/MySQL优化器原理.html-44cdf1f3.js" as="script"><link rel="prefetch" href="/assets/5种IO模型.html-623f6d24.js" as="script"><link rel="prefetch" href="/assets/Netty的线程模型.html-1933240c.js" as="script"><link rel="prefetch" href="/assets/粘包拆包问题.html-e5c58280.js" as="script"><link rel="prefetch" href="/assets/AOF和RDB持久化.html-de83ad53.js" as="script"><link rel="prefetch" href="/assets/Gossip协议.html-9cc1d704.js" as="script"><link rel="prefetch" href="/assets/Redisson看门狗机制.html-8e4e6651.js" as="script"><link rel="prefetch" href="/assets/主从复制原理.html-5692886d.js" as="script"><link rel="prefetch" href="/assets/内存淘汰策略.html-2108cb44.js" as="script"><link rel="prefetch" href="/assets/缓存击穿、穿透、雪崩.html-d043509c.js" as="script"><link rel="prefetch" href="/assets/过期键的删除策略.html-86aae1a3.js" as="script"><link rel="prefetch" href="/assets/消息发送.html-4164a815.js" as="script"><link rel="prefetch" href="/assets/消息持久化.html-ad4aec32.js" as="script"><link rel="prefetch" href="/assets/Spring4.3.x源码阅读.html-ce67a2f8.js" as="script"><link rel="prefetch" href="/assets/过滤器和拦截器的区别.html-c5dbe180.js" as="script"><link rel="prefetch" href="/assets/MySQL安装.html-ce91cf4e.js" as="script"><link rel="prefetch" href="/assets/nvm_node安装.html-c02c4943.js" as="script"><link rel="prefetch" href="/assets/xxl-job源码阅读.html-4a3b4336.js" as="script"><link rel="prefetch" href="/assets/算法补充题.html-82552fdf.js" as="script"><link rel="prefetch" href="/assets/转换函数.html-4e846421.js" as="script"><link rel="prefetch" href="/assets/分布式事务.html-4015f1f4.js" as="script"><link rel="prefetch" href="/assets/深入理解Java虚拟机.html-f58d9b73.js" as="script"><link rel="prefetch" href="/assets/Redis开发与运维.html-8fbbb19d.js" as="script"><link rel="prefetch" href="/assets/Redis设计与实现.html-3d2f28fe.js" as="script"><link rel="prefetch" href="/assets/中级开发（5年内） .html-fe02712d.js" as="script"><link rel="prefetch" href="/assets/初级开发（3年内）.html-669c578d.js" as="script"><link rel="prefetch" href="/assets/高级开发（8年内）.html-8e45d1a0.js" as="script"><link rel="prefetch" href="/assets/apache poi导致的oom.html-ac4f4186.js" as="script"><link rel="prefetch" href="/assets/Spring启动及扫描流程.html-dca86884.js" as="script"><link rel="prefetch" href="/assets/404.html-81a48005.js" as="script"><link rel="prefetch" href="/assets/index.html-2b6ad090.js" as="script"><link rel="prefetch" href="/assets/index.html-30245415.js" as="script"><link rel="prefetch" href="/assets/index.html-c2c088b3.js" as="script"><link rel="prefetch" href="/assets/index.html-f512ec67.js" as="script"><link rel="prefetch" href="/assets/index.html-e1891597.js" as="script"><link rel="prefetch" href="/assets/index.html-b77929dc.js" as="script"><link rel="prefetch" href="/assets/index.html-3e3e3234.js" as="script"><link rel="prefetch" href="/assets/index.html-db16fd88.js" as="script"><link rel="prefetch" href="/assets/index.html-02e59ce2.js" as="script"><link rel="prefetch" href="/assets/index.html-55c7e4fd.js" as="script"><link rel="prefetch" href="/assets/index.html-3b089dfb.js" as="script"><link rel="prefetch" href="/assets/index.html-ac5b8d61.js" as="script"><link rel="prefetch" href="/assets/index.html-a2f3ea26.js" as="script"><link rel="prefetch" href="/assets/index.html-b57e51cb.js" as="script"><link rel="prefetch" href="/assets/index.html-0c321714.js" as="script"><link rel="prefetch" href="/assets/index.html-6266e407.js" as="script"><link rel="prefetch" href="/assets/index.html-00f53358.js" as="script"><link rel="prefetch" href="/assets/index.html-7b46e405.js" as="script"><link rel="prefetch" href="/assets/index.html-219b1b1f.js" as="script"><link rel="prefetch" href="/assets/index.html-ecf569c4.js" as="script"><link rel="prefetch" href="/assets/aboutMe.html-a5377517.js" as="script"><link rel="prefetch" href="/assets/index.html-df94614a.js" as="script"><link rel="prefetch" href="/assets/Java集合.html-25980358.js" as="script"><link rel="prefetch" href="/assets/MyBatis常见问题.html-13559d56.js" as="script"><link rel="prefetch" href="/assets/InnoDB的Buffer Pool.html-2608d2bb.js" as="script"><link rel="prefetch" href="/assets/InnoDB的行格式和页结构.html-b66829d8.js" as="script"><link rel="prefetch" href="/assets/MySQL中的各种锁.html-eda1f1e8.js" as="script"><link rel="prefetch" href="/assets/MySQL中的排序.html-155835e9.js" as="script"><link rel="prefetch" href="/assets/MySQL事务与MVCC.html-5203098d.js" as="script"><link rel="prefetch" href="/assets/MySQL优化器原理.html-580192b8.js" as="script"><link rel="prefetch" href="/assets/5种IO模型.html-e14d32e5.js" as="script"><link rel="prefetch" href="/assets/Netty的线程模型.html-79ec3f0d.js" as="script"><link rel="prefetch" href="/assets/粘包拆包问题.html-ad117100.js" as="script"><link rel="prefetch" href="/assets/AOF和RDB持久化.html-8f530297.js" as="script"><link rel="prefetch" href="/assets/Gossip协议.html-68ce5f80.js" as="script"><link rel="prefetch" href="/assets/Redisson看门狗机制.html-f806e761.js" as="script"><link rel="prefetch" href="/assets/主从复制原理.html-c35d3468.js" as="script"><link rel="prefetch" href="/assets/内存淘汰策略.html-58163b51.js" as="script"><link rel="prefetch" href="/assets/缓存击穿、穿透、雪崩.html-d8d78972.js" as="script"><link rel="prefetch" href="/assets/过期键的删除策略.html-b2be5f51.js" as="script"><link rel="prefetch" href="/assets/消息发送.html-08920331.js" as="script"><link rel="prefetch" href="/assets/消息持久化.html-32746c2c.js" as="script"><link rel="prefetch" href="/assets/Spring4.3.x源码阅读.html-3475e320.js" as="script"><link rel="prefetch" href="/assets/过滤器和拦截器的区别.html-56c13477.js" as="script"><link rel="prefetch" href="/assets/MySQL安装.html-c7803a3a.js" as="script"><link rel="prefetch" href="/assets/nvm_node安装.html-736601b9.js" as="script"><link rel="prefetch" href="/assets/xxl-job源码阅读.html-2b98669d.js" as="script"><link rel="prefetch" href="/assets/算法补充题.html-37debf9d.js" as="script"><link rel="prefetch" href="/assets/转换函数.html-44dfb333.js" as="script"><link rel="prefetch" href="/assets/分布式事务.html-e6634dbe.js" as="script"><link rel="prefetch" href="/assets/深入理解Java虚拟机.html-3810cea1.js" as="script"><link rel="prefetch" href="/assets/Redis开发与运维.html-76744132.js" as="script"><link rel="prefetch" href="/assets/Redis设计与实现.html-eccc56a7.js" as="script"><link rel="prefetch" href="/assets/中级开发（5年内） .html-a2fc4d16.js" as="script"><link rel="prefetch" href="/assets/初级开发（3年内）.html-e48a4104.js" as="script"><link rel="prefetch" href="/assets/高级开发（8年内）.html-9138cbdd.js" as="script"><link rel="prefetch" href="/assets/apache poi导致的oom.html-b39dacda.js" as="script"><link rel="prefetch" href="/assets/Spring启动及扫描流程.html-0b604fff.js" as="script"><link rel="prefetch" href="/assets/404.html-742f0851.js" as="script"><link rel="prefetch" href="/assets/index.html-160c66d3.js" as="script"><link rel="prefetch" href="/assets/index.html-bb7b1e80.js" as="script"><link rel="prefetch" href="/assets/index.html-116fe317.js" as="script"><link rel="prefetch" href="/assets/index.html-17f24df8.js" as="script"><link rel="prefetch" href="/assets/index.html-3fe5d15e.js" as="script"><link rel="prefetch" href="/assets/index.html-c894bb5e.js" as="script"><link rel="prefetch" href="/assets/index.html-25b31507.js" as="script"><link rel="prefetch" href="/assets/index.html-8bb87106.js" as="script"><link rel="prefetch" href="/assets/index.html-255fc5d5.js" as="script"><link rel="prefetch" href="/assets/index.html-373a73ca.js" as="script"><link rel="prefetch" href="/assets/index.html-d539e1a9.js" as="script"><link rel="prefetch" href="/assets/index.html-1475787c.js" as="script"><link rel="prefetch" href="/assets/index.html-8073adbe.js" as="script"><link rel="prefetch" href="/assets/index.html-2ff9332c.js" as="script"><link rel="prefetch" href="/assets/index.html-f14ceb6a.js" as="script"><link rel="prefetch" href="/assets/index.html-54082429.js" as="script"><link rel="prefetch" href="/assets/index.html-264a3de2.js" as="script"><link rel="prefetch" href="/assets/index.html-fb62746d.js" as="script"><link rel="prefetch" href="/assets/index.html-81d28dad.js" as="script"><link rel="prefetch" href="/assets/index.html-56062662.js" as="script"><link rel="prefetch" href="/assets/giscus-0b7adcf8.js" as="script"><link rel="prefetch" href="/assets/photoswipe.esm-1464cdb9.js" as="script"><link rel="prefetch" href="/assets/index-e32a7948.js" as="script">
  </head>
  <body>
    <div id="app"><!--[--><!--[--><!--[--><span tabindex="-1"></span><a href="#main-content" class="vp-skip-link sr-only">跳至主要內容</a><!--]--><!--[--><div class="theme-container has-toc"><!--[--><header id="navbar" class="vp-navbar"><div class="vp-navbar-start"><button type="button" class="vp-toggle-sidebar-button" title="Toggle Sidebar"><span class="icon"></span></button><!--[--><!----><!--]--><!--[--><a class="vp-link vp-brand vp-brand" href="/"><img class="vp-nav-logo" src="https://gitee.com/eddie-lucas/images/raw/master/img/logo.svg" alt="小刘Learning"><!----><span class="vp-site-name hide-in-pad">小刘Learning</span></a><!--]--><!--[--><!----><!--]--></div><div class="vp-navbar-center"><!--[--><!----><!--]--><!--[--><nav class="vp-nav-links"><div class="nav-item hide-in-mobile"><a aria-label="首页" class="vp-link nav-link nav-link" href="/"><span class="font-icon icon iconfont icon-shouye" style=""></span>首页<!----></a></div><div class="nav-item hide-in-mobile"><a aria-label="后端技术" class="vp-link nav-link nav-link" href="/backend/mysql/MySQL%E4%BA%8B%E5%8A%A1%E4%B8%8EMVCC.html"><span class="font-icon icon iconfont icon-xingqiu" style=""></span>后端技术<!----></a></div><div class="nav-item hide-in-mobile"><a aria-label="读书笔记" class="vp-link nav-link nav-link" href="/readNote/redis/Redis%E5%BC%80%E5%8F%91%E4%B8%8E%E8%BF%90%E7%BB%B4.html"><span class="font-icon icon iconfont icon-book" style=""></span>读书笔记<!----></a></div></nav><!--]--><!--[--><!----><!--]--></div><div class="vp-navbar-end"><!--[--><!----><!--]--><!--[--><!----><div class="nav-item vp-repo"><a class="vp-repo-link" href="https://github.com/eddie-lucas" target="_blank" rel="noopener noreferrer" aria-label="GitHub"><svg xmlns="http://www.w3.org/2000/svg" class="icon github-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="github icon" style="width:1.25rem;height:1.25rem;vertical-align:middle;"><path d="M511.957 21.333C241.024 21.333 21.333 240.981 21.333 512c0 216.832 140.544 400.725 335.574 465.664 24.49 4.395 32.256-10.07 32.256-23.083 0-11.69.256-44.245 0-85.205-136.448 29.61-164.736-64.64-164.736-64.64-22.315-56.704-54.4-71.765-54.4-71.765-44.587-30.464 3.285-29.824 3.285-29.824 49.195 3.413 75.179 50.517 75.179 50.517 43.776 75.008 114.816 53.333 142.762 40.79 4.523-31.66 17.152-53.377 31.19-65.537-108.971-12.458-223.488-54.485-223.488-242.602 0-53.547 19.114-97.323 50.517-131.67-5.035-12.33-21.93-62.293 4.779-129.834 0 0 41.258-13.184 134.912 50.346a469.803 469.803 0 0 1 122.88-16.554c41.642.213 83.626 5.632 122.88 16.554 93.653-63.488 134.784-50.346 134.784-50.346 26.752 67.541 9.898 117.504 4.864 129.834 31.402 34.347 50.474 78.123 50.474 131.67 0 188.586-114.73 230.016-224.042 242.09 17.578 15.232 33.578 44.672 33.578 90.454v135.85c0 13.142 7.936 27.606 32.854 22.87C862.25 912.597 1002.667 728.747 1002.667 512c0-271.019-219.648-490.667-490.71-490.667z"></path></svg></a></div><div class="nav-item hide-in-mobile"><button type="button" id="appearance-switch"><svg xmlns="http://www.w3.org/2000/svg" class="icon auto-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="auto icon" style="display:block;"><path d="M512 992C246.92 992 32 777.08 32 512S246.92 32 512 32s480 214.92 480 480-214.92 480-480 480zm0-840c-198.78 0-360 161.22-360 360 0 198.84 161.22 360 360 360s360-161.16 360-360c0-198.78-161.22-360-360-360zm0 660V212c165.72 0 300 134.34 300 300 0 165.72-134.28 300-300 300z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon dark-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="dark icon" style="display:none;"><path d="M524.8 938.667h-4.267a439.893 439.893 0 0 1-313.173-134.4 446.293 446.293 0 0 1-11.093-597.334A432.213 432.213 0 0 1 366.933 90.027a42.667 42.667 0 0 1 45.227 9.386 42.667 42.667 0 0 1 10.24 42.667 358.4 358.4 0 0 0 82.773 375.893 361.387 361.387 0 0 0 376.747 82.774 42.667 42.667 0 0 1 54.187 55.04 433.493 433.493 0 0 1-99.84 154.88 438.613 438.613 0 0 1-311.467 128z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" class="icon light-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="light icon" style="display:none;"><path d="M952 552h-80a40 40 0 0 1 0-80h80a40 40 0 0 1 0 80zM801.88 280.08a41 41 0 0 1-57.96-57.96l57.96-58a41.04 41.04 0 0 1 58 58l-58 57.96zM512 752a240 240 0 1 1 0-480 240 240 0 0 1 0 480zm0-560a40 40 0 0 1-40-40V72a40 40 0 0 1 80 0v80a40 40 0 0 1-40 40zm-289.88 88.08-58-57.96a41.04 41.04 0 0 1 58-58l57.96 58a41 41 0 0 1-57.96 57.96zM192 512a40 40 0 0 1-40 40H72a40 40 0 0 1 0-80h80a40 40 0 0 1 40 40zm30.12 231.92a41 41 0 0 1 57.96 57.96l-57.96 58a41.04 41.04 0 0 1-58-58l58-57.96zM512 832a40 40 0 0 1 40 40v80a40 40 0 0 1-80 0v-80a40 40 0 0 1 40-40zm289.88-88.08 58 57.96a41.04 41.04 0 0 1-58 58l-57.96-58a41 41 0 0 1 57.96-57.96z"></path></svg></button></div><!----><!--]--><!--[--><!----><!--]--><button type="button" class="vp-toggle-navbar-button" aria-label="Toggle Navbar" aria-expanded="false" aria-controls="nav-screen"><span><span class="vp-top"></span><span class="vp-middle"></span><span class="vp-bottom"></span></span></button></div></header><!----><!--]--><!----><div class="toggle-sidebar-wrapper"><span class="arrow start"></span></div><aside id="sidebar" class="vp-sidebar"><!--[--><!----><!--]--><ul class="vp-sidebar-links"><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-MySQL" style=""></span><span class="vp-sidebar-title">MySQL</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-redis" style=""></span><span class="vp-sidebar-title">Redis</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-Spring" style=""></span><span class="vp-sidebar-title">Spring</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-a-icons8-mybatis2" style=""></span><span class="vp-sidebar-title">MyBatis</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-xxl-job" style=""></span><span class="vp-sidebar-title">XXL-JOB</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable active" type="button"><span class="font-icon icon iconfont icon-RocketMQ" style=""></span><span class="vp-sidebar-title">RocketMQ 4.6.0</span><span class="vp-arrow down"></span></button><ul class="vp-sidebar-links"><li><!--[--><a aria-label="NameServer" class="vp-link nav-link active vp-sidebar-link vp-sidebar-page active nav-link active vp-sidebar-link vp-sidebar-page active" href="/backend/rocketmq/NameServer.html"><!---->NameServer<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="启动流程" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#启动流程"><!---->启动流程<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="实例化NameSrvController" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#实例化namesrvcontroller"><!---->实例化NameSrvController<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="初始化NameSrvController" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#初始化namesrvcontroller"><!---->初始化NameSrvController<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="总结" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#总结"><!---->总结<!----></a><ul class="vp-sidebar-sub-headers"></ul></li></ul></li><li class="vp-sidebar-sub-header"><a aria-label="路由注册" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#路由注册"><!---->路由注册<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="broker发送心跳" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#broker发送心跳"><!---->broker发送心跳<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="NameServer处理心跳" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#nameserver处理心跳"><!---->NameServer处理心跳<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="设计亮点" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#设计亮点"><!---->设计亮点<!----></a><ul class="vp-sidebar-sub-headers"></ul></li></ul></li><li class="vp-sidebar-sub-header"><a aria-label="路由删除" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#路由删除"><!---->路由删除<!----></a><ul class="vp-sidebar-sub-headers"><li class="vp-sidebar-sub-header"><a aria-label="定时扫描删除" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#定时扫描删除"><!---->定时扫描删除<!----></a><ul class="vp-sidebar-sub-headers"></ul></li><li class="vp-sidebar-sub-header"><a aria-label="broker正常关闭删除" class="vp-link nav-link vp-sidebar-link vp-heading nav-link vp-sidebar-link vp-heading" href="/backend/rocketmq/NameServer.html#broker正常关闭删除"><!---->broker正常关闭删除<!----></a><ul class="vp-sidebar-sub-headers"></ul></li></ul></li></ul><!--]--></li><li><!--[--><a aria-label="消息发送" class="vp-link nav-link vp-sidebar-link vp-sidebar-page nav-link vp-sidebar-link vp-sidebar-page" href="/backend/rocketmq/%E6%B6%88%E6%81%AF%E5%8F%91%E9%80%81.html"><!---->消息发送<!----></a><ul class="vp-sidebar-sub-headers"></ul><!--]--></li><li><!--[--><a aria-label="消息持久化" class="vp-link nav-link vp-sidebar-link vp-sidebar-page nav-link vp-sidebar-link vp-sidebar-page" href="/backend/rocketmq/%E6%B6%88%E6%81%AF%E6%8C%81%E4%B9%85%E5%8C%96.html"><!---->消息持久化<!----></a><ul class="vp-sidebar-sub-headers"></ul><!--]--></li></ul></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-Netty" style=""></span><span class="vp-sidebar-title">Netty</span><span class="vp-arrow end"></span></button><!----></section></li><li><section class="vp-sidebar-group"><button class="vp-sidebar-heading clickable" type="button"><span class="font-icon icon iconfont icon-tools" style=""></span><span class="vp-sidebar-title">工具安装教程</span><span class="vp-arrow end"></span></button><!----></section></li></ul><!--[--><!----><!--]--></aside><!--[--><main id="main-content" class="vp-page"><!--[--><!--[--><!----><!--]--><!----><nav class="vp-breadcrumb disable"></nav><div class="vp-page-title"><h1><!---->NameServer</h1><div class="page-info"><span class="page-author-info" aria-label="作者🖊" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon author-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="author icon"><path d="M649.6 633.6c86.4-48 147.2-144 147.2-249.6 0-160-128-288-288-288s-288 128-288 288c0 108.8 57.6 201.6 147.2 249.6-121.6 48-214.4 153.6-240 288-3.2 9.6 0 19.2 6.4 25.6 3.2 9.6 12.8 12.8 22.4 12.8h704c9.6 0 19.2-3.2 25.6-12.8 6.4-6.4 9.6-16 6.4-25.6-25.6-134.4-121.6-240-243.2-288z"></path></svg><span><span class="page-author-item">小刘Learning</span></span><span property="author" content="小刘Learning"></span></span><!----><span class="page-date-info" aria-label="写作日期📅" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon calendar-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="calendar icon"><path d="M716.4 110.137c0-18.753-14.72-33.473-33.472-33.473-18.753 0-33.473 14.72-33.473 33.473v33.473h66.993v-33.473zm-334.87 0c0-18.753-14.72-33.473-33.473-33.473s-33.52 14.72-33.52 33.473v33.473h66.993v-33.473zm468.81 33.52H716.4v100.465c0 18.753-14.72 33.473-33.472 33.473a33.145 33.145 0 01-33.473-33.473V143.657H381.53v100.465c0 18.753-14.72 33.473-33.473 33.473a33.145 33.145 0 01-33.473-33.473V143.657H180.6A134.314 134.314 0 0046.66 277.595v535.756A134.314 134.314 0 00180.6 947.289h669.74a134.36 134.36 0 00133.94-133.938V277.595a134.314 134.314 0 00-133.94-133.938zm33.473 267.877H147.126a33.145 33.145 0 01-33.473-33.473c0-18.752 14.72-33.473 33.473-33.473h736.687c18.752 0 33.472 14.72 33.472 33.473a33.145 33.145 0 01-33.472 33.473z"></path></svg><span><!----></span><meta property="datePublished" content="2024-08-16T09:13:59.000Z"></span><!----><span class="page-reading-time-info" aria-label="阅读时间⌛" data-balloon-pos="down"><svg xmlns="http://www.w3.org/2000/svg" class="icon timer-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="timer icon"><path d="M799.387 122.15c4.402-2.978 7.38-7.897 7.38-13.463v-1.165c0-8.933-7.38-16.312-16.312-16.312H256.33c-8.933 0-16.311 7.38-16.311 16.312v1.165c0 5.825 2.977 10.874 7.637 13.592 4.143 194.44 97.22 354.963 220.201 392.763-122.204 37.542-214.893 196.511-220.2 389.397-4.661 5.049-7.638 11.651-7.638 19.03v5.825h566.49v-5.825c0-7.379-2.849-13.981-7.509-18.9-5.049-193.016-97.867-351.985-220.2-389.527 123.24-37.67 216.446-198.453 220.588-392.892zM531.16 450.445v352.632c117.674 1.553 211.787 40.778 211.787 88.676H304.097c0-48.286 95.149-87.382 213.728-88.676V450.445c-93.077-3.107-167.901-81.297-167.901-177.093 0-8.803 6.99-15.793 15.793-15.793 8.803 0 15.794 6.99 15.794 15.793 0 80.261 63.69 145.635 142.01 145.635s142.011-65.374 142.011-145.635c0-8.803 6.99-15.793 15.794-15.793s15.793 6.99 15.793 15.793c0 95.019-73.789 172.82-165.96 177.093z"></path></svg><span>大约 14 分钟</span><meta property="timeRequired" content="PT14M"></span><!----><!----></div><hr></div><div class="toc-place-holder"><aside id="toc"><!--[--><!----><!--]--><div class="toc-header">此页内容<button type="button" class="print-button" title="打印"><svg xmlns="http://www.w3.org/2000/svg" class="icon print-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="print icon"><path d="M819.2 364.8h-44.8V128c0-17.067-14.933-32-32-32H281.6c-17.067 0-32 14.933-32 32v236.8h-44.8C145.067 364.8 96 413.867 96 473.6v192c0 59.733 49.067 108.8 108.8 108.8h44.8V896c0 17.067 14.933 32 32 32h460.8c17.067 0 32-14.933 32-32V774.4h44.8c59.733 0 108.8-49.067 108.8-108.8v-192c0-59.733-49.067-108.8-108.8-108.8zM313.6 160h396.8v204.8H313.6V160zm396.8 704H313.6V620.8h396.8V864zM864 665.6c0 25.6-19.2 44.8-44.8 44.8h-44.8V588.8c0-17.067-14.933-32-32-32H281.6c-17.067 0-32 14.933-32 32v121.6h-44.8c-25.6 0-44.8-19.2-44.8-44.8v-192c0-25.6 19.2-44.8 44.8-44.8h614.4c25.6 0 44.8 19.2 44.8 44.8v192z"></path></svg></button></div><div class="toc-wrapper"><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#启动流程">启动流程</a></li><li><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#实例化namesrvcontroller">实例化NameSrvController</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#初始化namesrvcontroller">初始化NameSrvController</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#总结">总结</a></li><!----><!--]--></ul></li><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#路由注册">路由注册</a></li><li><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#broker发送心跳">broker发送心跳</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#nameserver处理心跳">NameServer处理心跳</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#设计亮点">设计亮点</a></li><!----><!--]--></ul></li><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level2 toc-link level2" href="#路由删除">路由删除</a></li><li><ul class="toc-list"><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#定时扫描删除">定时扫描删除</a></li><!----><!--]--><!--[--><li class="toc-item"><a class="vp-link toc-link level3 toc-link level3" href="#broker正常关闭删除">broker正常关闭删除</a></li><!----><!--]--></ul></li><!--]--></ul><div class="toc-marker" style="top:-1.7rem;"></div></div><!--[--><!----><!--]--></aside></div><!--[--><!----><!--]--><div class="theme-hope-content"><p>NameServer是整个RocketMQ的大脑，它主要负责RocketMQ的路由管理，服务注册，服务发现。</p><p>消息中间件一般基于主题的发布订阅机制，生产者发送某一主题的消息到MQ，MQ负责该消息的持久化存储，消费者订阅感兴趣的主题，对消息进行消费</p><p>为了避免单个MQ瘫痪而导致整个系统瘫痪，通常会采用MQ集群，那么生产者如何知道消息要发往哪个MQ？如果某一台MQ宕机了，生产者如何在不重启服务的情况下感知？</p><p>NameServer就是为了解决上述问题设计的</p><figure><img src="/assets/image-20231123171742673-5e8bd922.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>MQ在启动时，向所有的NameServer注册，消息生产者在发送消息之前先从NameServer获取Broker服务器（MQ）的地址列表，然后根据负载算法从列表中选择一台消息服务器发送消息。NameServer与每台Broker服务器保持长连接，并<strong>间隔10s检测Broker是否存活</strong>，如果检测到Broker宕机，则从路由注册表中将其移除，但是路由变化不会马上通知消息生产者</p><p>NameServer本身的高可用性可通过部署多台NameServer服务器来实现，但<strong>彼此之间互不通信</strong>。虽然NameServer服务器之间在某一时刻的数据并不会完全相同，但对消息发送不会造成重大影响，无非就是短暂造成消息发送不均衡</p><figure><img src="/assets/image-20231123173100756-8cef721b.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><ul><li>Broker每隔30s向NameServer集群的每一台机器发送心跳包，包含自身创建的topic路由等信息。</li><li>消息客户端每隔30s向NameServer更新对应topic的路由信息。</li><li>NameServer收到Broker发送的心跳包时会记录时间戳。</li><li>NameServer每隔10s会扫描一次brokerLiveTable（存放心跳包的时间戳信息），如果在120s内没有收到心跳包，则认为Broker失效，更新topic的路由信息，将失效的Broker信息移除。</li></ul><h2 id="启动流程" tabindex="-1"><a class="header-anchor" href="#启动流程" aria-hidden="true">#</a> 启动流程</h2><p>当NameServer启动的时候，具体做了哪些事情？在NameServer的启动类中，主要做了两件事情：</p><ol><li>创建了一个NameSrvController，即NameServer控制器</li><li>启动NameSrvController</li></ol><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">NamesrvController</span> <span class="token function">main0</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        
            <span class="token comment">//创建一个NamesrvController</span>
            <span class="token class-name">NamesrvController</span> controller <span class="token operator">=</span> <span class="token function">createNamesrvController</span><span class="token punctuation">(</span>args<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//启动NamesrvController</span>
            <span class="token function">start</span><span class="token punctuation">(</span>controller<span class="token punctuation">)</span><span class="token punctuation">;</span>
    		<span class="token comment">//打印启动成功提示。。。</span>
            <span class="token keyword">return</span> controller<span class="token punctuation">;</span>
       
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="实例化namesrvcontroller" tabindex="-1"><a class="header-anchor" href="#实例化namesrvcontroller" aria-hidden="true">#</a> 实例化NameSrvController</h3><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code>    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">NamesrvController</span> <span class="token function">createNamesrvController</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">IOException</span><span class="token punctuation">,</span> <span class="token class-name">JoranException</span> <span class="token punctuation">{</span>

        <span class="token comment">//NameServer的业务参数</span>
        <span class="token keyword">final</span> <span class="token class-name">NamesrvConfig</span> namesrvConfig <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NamesrvConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//NameServer的网络参数</span>
        <span class="token keyword">final</span> <span class="token class-name">NettyServerConfig</span> nettyServerConfig <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NettyServerConfig</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//默认端口</span>
        nettyServerConfig<span class="token punctuation">.</span><span class="token function">setListenPort</span><span class="token punctuation">(</span><span class="token number">9876</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//可以通过-c指定nameserver的配置文件</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>commandLine<span class="token punctuation">.</span><span class="token function">hasOption</span><span class="token punctuation">(</span><span class="token char">&#39;c&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">String</span> file <span class="token operator">=</span> commandLine<span class="token punctuation">.</span><span class="token function">getOptionValue</span><span class="token punctuation">(</span><span class="token char">&#39;c&#39;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>file <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">InputStream</span> in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedInputStream</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileInputStream</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">properties2Object</span><span class="token punctuation">(</span>properties<span class="token punctuation">,</span> namesrvConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">properties2Object</span><span class="token punctuation">(</span>properties<span class="token punctuation">,</span> nettyServerConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//将默认路径修改为指定的路径</span>
                namesrvConfig<span class="token punctuation">.</span><span class="token function">setConfigStorePath</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;load config properties file OK, %s%n&quot;</span><span class="token punctuation">,</span> file<span class="token punctuation">)</span><span class="token punctuation">;</span>
                in<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//可以通过-p打印当前加载的配置</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>commandLine<span class="token punctuation">.</span><span class="token function">hasOption</span><span class="token punctuation">(</span><span class="token char">&#39;p&#39;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">InternalLogger</span> console <span class="token operator">=</span> <span class="token class-name">InternalLoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token class-name">LoggerName</span><span class="token punctuation">.</span><span class="token constant">NAMESRV_CONSOLE_NAME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">printObjectProperties</span><span class="token punctuation">(</span>console<span class="token punctuation">,</span> namesrvConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">printObjectProperties</span><span class="token punctuation">(</span>console<span class="token punctuation">,</span> nettyServerConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">properties2Object</span><span class="token punctuation">(</span><span class="token class-name">ServerUtil</span><span class="token punctuation">.</span><span class="token function">commandLine2Properties</span><span class="token punctuation">(</span>commandLine<span class="token punctuation">)</span><span class="token punctuation">,</span> namesrvConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> namesrvConfig<span class="token punctuation">.</span><span class="token function">getRocketmqHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;Please set the %s variable in your environment to match the location of the RocketMQ installation%n&quot;</span><span class="token punctuation">,</span> <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token constant">ROCKETMQ_HOME_ENV</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">exit</span><span class="token punctuation">(</span><span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">//日志相关配置</span>
        <span class="token class-name">LoggerContext</span> lc <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">LoggerContext</span><span class="token punctuation">)</span> <span class="token class-name">LoggerFactory</span><span class="token punctuation">.</span><span class="token function">getILoggerFactory</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">JoranConfigurator</span> configurator <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">JoranConfigurator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        configurator<span class="token punctuation">.</span><span class="token function">setContext</span><span class="token punctuation">(</span>lc<span class="token punctuation">)</span><span class="token punctuation">;</span>
        lc<span class="token punctuation">.</span><span class="token function">reset</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        configurator<span class="token punctuation">.</span><span class="token function">doConfigure</span><span class="token punctuation">(</span>namesrvConfig<span class="token punctuation">.</span><span class="token function">getRocketmqHome</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">+</span> <span class="token string">&quot;/conf/logback_namesrv.xml&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        log <span class="token operator">=</span> <span class="token class-name">InternalLoggerFactory</span><span class="token punctuation">.</span><span class="token function">getLogger</span><span class="token punctuation">(</span><span class="token class-name">LoggerName</span><span class="token punctuation">.</span><span class="token constant">NAMESRV_LOGGER_NAME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">printObjectProperties</span><span class="token punctuation">(</span>log<span class="token punctuation">,</span> namesrvConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token function">printObjectProperties</span><span class="token punctuation">(</span>log<span class="token punctuation">,</span> nettyServerConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//通过业务参数和网络参数创建一个NamesrvController</span>
        <span class="token keyword">final</span> <span class="token class-name">NamesrvController</span> controller <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NamesrvController</span><span class="token punctuation">(</span>namesrvConfig<span class="token punctuation">,</span> nettyServerConfig<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//记住所有属性，防止丢弃</span>
        controller<span class="token punctuation">.</span><span class="token function">getConfiguration</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">registerConfig</span><span class="token punctuation">(</span>properties<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> controller<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以看出，主要是通过<code>业务参数</code>和<code>网络参数</code>来创建一个controller，那么业务参数和网络参数有哪些？</p><p>业务参数：</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NamesrvConfig</span> <span class="token punctuation">{</span>
    <span class="token doc-comment comment">/**rocketmq的主目录，有两种方式设置
     * 1. -D rocketmq.home.dir=path（优先级更高）
     * 2. 设置环境变量ROCKETMQ_HOME
     * */</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> rocketmqHome<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**NameServer存储KV配置属性的持久化路径*/</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> kvConfigPath<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**nameserver默认配置文件路径，可以通过-c指定*/</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> configStorePath<span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token class-name">String</span> productEnvName <span class="token operator">=</span> <span class="token string">&quot;center&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> clusterTest <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**是否支持顺序消息，默认不支持*/</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> orderMessageEnable <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>网络参数：</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">NettyServerConfig</span> <span class="token keyword">implements</span> <span class="token class-name">Cloneable</span> <span class="token punctuation">{</span>
    <span class="token comment">//监听端口</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> listenPort<span class="token punctuation">;</span>
    <span class="token comment">//业务线程池线程数</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverWorkerThreads <span class="token operator">=</span> <span class="token number">8</span><span class="token punctuation">;</span>

    <span class="token comment">//public任务线程池线程个数</span>
    <span class="token doc-comment comment">/**
     * Netty⽹络会根据业务类型创建不同的线程池，⽐如处
     * 理消息发送、消息消费、⼼跳检测等。如果该业务类型
     * （RequestCode）未注册线程池，则由public线程池执⾏。
     * */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverCallbackExecutorThreads <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * I/O线程池线程个数，主要是
     * NameServer、Broker端解析请求、返回相应的线程个数。这类线程
     * 主要⽤于处理⽹络请求，先解析请求包，然后转发到各个业务线程池
     * 完成具体的业务操作，最后将结果返回给调⽤⽅
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverSelectorThreads <span class="token operator">=</span> <span class="token number">3</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * send oneway消息请求的并发度（Broker端参数）
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverOnewaySemaphoreValue <span class="token operator">=</span> <span class="token number">256</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * 异步消息发送的最⼤并发度（Broker端参数）
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverAsyncSemaphoreValue <span class="token operator">=</span> <span class="token number">64</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * ⽹络连接最⼤空闲时间，默认为120s。如果连接空闲时间超过该参数设置的值，连接将被关闭
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverChannelMaxIdleTimeSeconds <span class="token operator">=</span> <span class="token number">120</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * ⽹络socket发送缓存区⼤⼩，默认为64KB
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverSocketSndBufSize <span class="token operator">=</span> <span class="token class-name">NettySystemConfig</span><span class="token punctuation">.</span>socketSndbufSize<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * ⽹络socket接收缓存区⼤⼩，默认为64KB
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">int</span> serverSocketRcvBufSize <span class="token operator">=</span> <span class="token class-name">NettySystemConfig</span><span class="token punctuation">.</span>socketRcvbufSize<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * ：ByteBuffer是否开启缓存，建议开启
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> serverPooledByteBufAllocatorEnable <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * 是否启⽤Epoll I/O模型，Linux环境下建议开启
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">boolean</span> useEpollNativeSelector <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="初始化namesrvcontroller" tabindex="-1"><a class="header-anchor" href="#初始化namesrvcontroller" aria-hidden="true">#</a> 初始化NameSrvController</h3><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">NamesrvController</span> <span class="token function">start</span><span class="token punctuation">(</span><span class="token keyword">final</span> <span class="token class-name">NamesrvController</span> controller<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>

        <span class="token comment">//初始化NamesrvController</span>
        <span class="token keyword">boolean</span> initResult <span class="token operator">=</span> controller<span class="token punctuation">.</span><span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//注册JVM钩⼦函数并启动服务器，以便监听Broker、消息⽣产者的⽹络请求</span>
        <span class="token class-name">Runtime</span><span class="token punctuation">.</span><span class="token function">getRuntime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">addShutdownHook</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">ShutdownHookThread</span><span class="token punctuation">(</span>log<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">Callable</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Void</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token class-name">Void</span> <span class="token function">call</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
                controller<span class="token punctuation">.</span><span class="token function">shutdown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">return</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        controller<span class="token punctuation">.</span><span class="token function">start</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">return</span> controller<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>可以看出<code>initialize()</code>是初始化的核心内容</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">boolean</span> <span class="token function">initialize</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//加载kv配置</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>kvConfigManager<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//创建一个网络处理对象</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>remotingServer <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">NettyRemotingServer</span><span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span>nettyServerConfig<span class="token punctuation">,</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerHousekeepingService<span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">this</span><span class="token punctuation">.</span>remotingExecutor <span class="token operator">=</span>
            <span class="token class-name">Executors</span><span class="token punctuation">.</span><span class="token function">newFixedThreadPool</span><span class="token punctuation">(</span>nettyServerConfig<span class="token punctuation">.</span><span class="token function">getServerWorkerThreads</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">ThreadFactoryImpl</span><span class="token punctuation">(</span><span class="token string">&quot;RemotingExecutorThread_&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">registerProcessor</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//定时任务：每隔10s检测brokerLiveTable中的broker心跳,移除不可用broker和相关的channel</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>scheduledExecutorService<span class="token punctuation">.</span><span class="token function">scheduleAtFixedRate</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">NamesrvController</span><span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>routeInfoManager<span class="token punctuation">.</span><span class="token function">scanNotActiveBroker</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">5</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">SECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//定时任务：每隔10min打印⼀次KV配置</span>
        <span class="token keyword">this</span><span class="token punctuation">.</span>scheduledExecutorService<span class="token punctuation">.</span><span class="token function">scheduleAtFixedRate</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">NamesrvController</span><span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>kvConfigManager<span class="token punctuation">.</span><span class="token function">printAllPeriodically</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">10</span><span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MINUTES</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

        <span class="token comment">//如果支持tls(SSL)安全通信方式</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">TlsSystemConfig</span><span class="token punctuation">.</span>tlsMode <span class="token operator">!=</span> <span class="token class-name">TlsMode</span><span class="token punctuation">.</span><span class="token constant">DISABLED</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// Register a listener to reload SslContext</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>在初始化阶段，启动了<strong>两个定时任务</strong>，一个定时检测心跳，一个定时打印kv配置</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">scanNotActiveBroker</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//遍历broker状态表，看看有没有要移除的broker</span>
        <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span></span> next <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">long</span> last <span class="token operator">=</span> next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getLastUpdateTimestamp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//比较当前时间与上一次时间，如果超过120s就认为broker不可用，从brokerLiveTable中移除</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>last <span class="token operator">+</span> <span class="token constant">BROKER_CHANNEL_EXPIRED_TIME</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">RemotingUtil</span><span class="token punctuation">.</span><span class="token function">closeChannel</span><span class="token punctuation">(</span>next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">warn</span><span class="token punctuation">(</span><span class="token string">&quot;The broker channel expired, {} {}ms&quot;</span><span class="token punctuation">,</span> next<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">BROKER_CHANNEL_EXPIRED_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//维护路由表以及其他信息（具体步骤）</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onChannelDestroy</span><span class="token punctuation">(</span>next<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="总结" tabindex="-1"><a class="header-anchor" href="#总结" aria-hidden="true">#</a> 总结</h3><p>在NameServer启动阶段，主要做了两个事情：</p><ul><li><p>实例化NameSrvController（根据业务参数和网络参数实例化）</p></li><li><p>初始化NameSrvController</p><ul><li>加载kv配置，这里的kv配置来源于业务参数中<code>kvConfigPath</code>路径的json文件</li><li>检测心跳定时任务（每10s）</li><li>打印配置定时任务（每10min）</li><li>注册JVM钩⼦函数并启动服务器，以便监听Broker、消息⽣产者的⽹络请求</li></ul></li></ul><h2 id="路由注册" tabindex="-1"><a class="header-anchor" href="#路由注册" aria-hidden="true">#</a> 路由注册</h2><p>NameServer的主要作用是为生产者和消费者提供关于topic的路由信息，那么NameServer就需要存储路由的基础信息，并且能够管理broker节点，包括路由注册，路由删除等功能</p><blockquote><p>NameServer存储了哪些信息？</p></blockquote><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">RouteInfoManager</span> <span class="token punctuation">{</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token keyword">static</span> <span class="token keyword">long</span> <span class="token constant">BROKER_CHANNEL_EXPIRED_TIME</span> <span class="token operator">=</span> <span class="token number">1000</span> <span class="token operator">*</span> <span class="token number">60</span> <span class="token operator">*</span> <span class="token number">2</span><span class="token punctuation">;</span>
    
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">ReadWriteLock</span> lock <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ReentrantReadWriteLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * topic消息队列的路由信息，消息发送时根据路由表进⾏负载均衡
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* topic */</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">QueueData</span><span class="token punctuation">&gt;</span></span><span class="token operator">&gt;</span> topicQueueTable<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * 主从broker地址表，存放主从Broker基础信息，包含brokerName、所属集群名称、主从Broker地址
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* brokerName */</span><span class="token punctuation">,</span> <span class="token class-name">BrokerData</span><span class="token operator">&gt;</span> brokerAddrTable<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * 集群地址表，存放Broker集群信息，存储集群中所有主从架构Broker的名称
     * 一个集群中会有很多个主从broker架构，依据brokerName区分，同一主从架构中，依据brokerId区分主从节点
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* clusterName */</span><span class="token punctuation">,</span> <span class="token class-name">Set</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* brokerName */</span><span class="token operator">&gt;&gt;</span> clusterAddrTable<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * broker状态表，存放Broker状态信息，NameServer每次收到⼼跳包时会替换该信息
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* brokerAddr */</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token operator">&gt;</span> brokerLiveTable<span class="token punctuation">;</span>
    <span class="token doc-comment comment">/**
     * Broker上的FilterServer列表，⽤于类模式消息过滤。类模式过滤机制在4.4及以后版本被废弃
     */</span>
    <span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">HashMap</span><span class="token operator">&lt;</span><span class="token class-name">String</span><span class="token comment">/* brokerAddr */</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token comment">/* Filter Server */</span><span class="token operator">&gt;</span> filterServerTable<span class="token punctuation">;</span>
    
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote><p>如何进行路由注册的？</p></blockquote><h3 id="broker发送心跳" tabindex="-1"><a class="header-anchor" href="#broker发送心跳" aria-hidden="true">#</a> broker发送心跳</h3><p>路由注册由broker和NameServer共同完成，RocketMQ路由注册是通过Broker与NameServer的⼼跳功能实现的。Broker启动时，<strong>每隔30s向集群中所有的NameServer发送心跳包</strong>，NameServer收到Broker⼼跳包时会先更新<code>brokerLiveTable</code>缓存中<code>BrokerLiveInfo</code>的lastUpdateTimestamp，然后每隔10s扫描⼀次brokerLiveTable，如果连续120s没有收到⼼跳包，NameServer将移除该Broker的路由信息，同时关闭连接</p><figure><img src="/assets/image-20231205142703933-4644f22e.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>从上图可以看出，broker端启动定时任务，<strong>每隔30s发送心跳包</strong>，broker发送心跳的核心代码位于<code>BrokerOuterAPI</code>类中</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">RegisterBrokerResult</span><span class="token punctuation">&gt;</span></span> <span class="token function">registerBrokerAll</span><span class="token punctuation">(</span>
            <span class="token comment">//集群名称</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> clusterName<span class="token punctuation">,</span>
        <span class="token comment">//broker地址</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> brokerAddr<span class="token punctuation">,</span>
        <span class="token comment">//broker名称</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> brokerName<span class="token punctuation">,</span>
        <span class="token comment">//broker的id</span>
        <span class="token keyword">final</span> <span class="token keyword">long</span> brokerId<span class="token punctuation">,</span>
        <span class="token comment">//主节点地址，初次请求时该值为空</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> haServerAddr<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">TopicConfigSerializeWrapper</span> topicConfigWrapper<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> filterServerList<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token keyword">boolean</span> oneway<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token keyword">int</span> timeoutMills<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token keyword">boolean</span> compressed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
        <span class="token comment">//准备一个list，接收结果</span>
        <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">RegisterBrokerResult</span><span class="token punctuation">&gt;</span></span> registerBrokerResultList <span class="token operator">=</span> <span class="token class-name">Lists</span><span class="token punctuation">.</span><span class="token function">newArrayList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//获取nameserver的地址</span>
        <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> nameServerAddressList <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>remotingClient<span class="token punctuation">.</span><span class="token function">getNameServerAddressList</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>nameServerAddressList <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> nameServerAddressList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token comment">//请求头信息</span>
            <span class="token keyword">final</span> <span class="token class-name">RegisterBrokerRequestHeader</span> requestHeader <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegisterBrokerRequestHeader</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setBrokerAddr</span><span class="token punctuation">(</span>brokerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setBrokerId</span><span class="token punctuation">(</span>brokerId<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setBrokerName</span><span class="token punctuation">(</span>brokerName<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setClusterName</span><span class="token punctuation">(</span>clusterName<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setHaServerAddr</span><span class="token punctuation">(</span>haServerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setCompressed</span><span class="token punctuation">(</span>compressed<span class="token punctuation">)</span><span class="token punctuation">;</span>

            <span class="token comment">//请求体信息</span>
            <span class="token class-name">RegisterBrokerBody</span> requestBody <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegisterBrokerBody</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestBody<span class="token punctuation">.</span><span class="token function">setTopicConfigSerializeWrapper</span><span class="token punctuation">(</span>topicConfigWrapper<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestBody<span class="token punctuation">.</span><span class="token function">setFilterServerList</span><span class="token punctuation">(</span>filterServerList<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">final</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> body <span class="token operator">=</span> requestBody<span class="token punctuation">.</span><span class="token function">encode</span><span class="token punctuation">(</span>compressed<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">final</span> <span class="token keyword">int</span> bodyCrc32 <span class="token operator">=</span> <span class="token class-name">UtilAll</span><span class="token punctuation">.</span><span class="token function">crc32</span><span class="token punctuation">(</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
            requestHeader<span class="token punctuation">.</span><span class="token function">setBodyCrc32</span><span class="token punctuation">(</span>bodyCrc32<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//减法计数器</span>
            <span class="token keyword">final</span> <span class="token class-name">CountDownLatch</span> countDownLatch <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">CountDownLatch</span><span class="token punctuation">(</span>nameServerAddressList<span class="token punctuation">.</span><span class="token function">size</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">final</span> <span class="token class-name">String</span> namesrvAddr <span class="token operator">:</span> nameServerAddressList<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                brokerOuterExecutor<span class="token punctuation">.</span><span class="token function">execute</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token annotation punctuation">@Override</span>
                    <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token class-name">RegisterBrokerResult</span> result <span class="token operator">=</span> <span class="token function">registerBroker</span><span class="token punctuation">(</span>namesrvAddr<span class="token punctuation">,</span>oneway<span class="token punctuation">,</span> timeoutMills<span class="token punctuation">,</span>requestHeader<span class="token punctuation">,</span>body<span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">if</span> <span class="token punctuation">(</span>result <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                registerBrokerResultList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>result<span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token punctuation">}</span>
                            countDownLatch<span class="token punctuation">.</span><span class="token function">countDown</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            countDownLatch<span class="token punctuation">.</span><span class="token function">await</span><span class="token punctuation">(</span>timeoutMills<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MILLISECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token keyword">return</span> registerBrokerResultList<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>该⽅法遍历NameServer列表，Broker依次向NameServer发送⼼跳包</p><h3 id="nameserver处理心跳" tabindex="-1"><a class="header-anchor" href="#nameserver处理心跳" aria-hidden="true">#</a> NameServer处理心跳</h3><p>RocketMQ网络传输基于Netty，在注册broker时，broker<strong>调用的是同步方法，即心跳包请求串行执行</strong>，对于每⼀个请求，RocketMQ都会定义⼀个RequestCode，对于注册broker，发送心跳，请求码为<code>RequestCode.REGISTER_BROKER</code>（值为103）</p><figure><img src="/assets/image-20231205150035390-26686e6b.png" alt="" tabindex="0" loading="lazy"><figcaption></figcaption></figure><p>对于这种请求，最终会被交给<code>RouteInfoManager</code>类处理，核心代码如下：</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token class-name">RegisterBrokerResult</span> <span class="token function">registerBroker</span><span class="token punctuation">(</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> clusterName<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> brokerAddr<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> brokerName<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token keyword">long</span> brokerId<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">String</span> haServerAddr<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">TopicConfigSerializeWrapper</span> topicConfigWrapper<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> filterServerList<span class="token punctuation">,</span>
        <span class="token keyword">final</span> <span class="token class-name">Channel</span> channel<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">RegisterBrokerResult</span> result <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">RegisterBrokerResult</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token comment">//加锁，防止并发修改</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lockInterruptibly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//clusterAddrTable信息维护</span>
                <span class="token comment">// ⾸先判断Broker所属集群是否存在，如果不存在，则创建集群，然后将broker名加⼊集群Broker集合</span>
                <span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> brokerNames <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>clusterAddrTable<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>clusterName<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> brokerNames<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    brokerNames <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">HashSet</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>clusterAddrTable<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>clusterName<span class="token punctuation">,</span> brokerNames<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                brokerNames<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>brokerName<span class="token punctuation">)</span><span class="token punctuation">;</span>


                <span class="token comment">//brokerData信息维护</span>
                <span class="token comment">//是否是第一次注册</span>
                <span class="token keyword">boolean</span> registerFirst <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
                <span class="token class-name">BrokerData</span> brokerData <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerAddrTable<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>brokerName<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//没有找到brokerData，说明是第一次注册</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> brokerData<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    registerFirst <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                    brokerData <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BrokerData</span><span class="token punctuation">(</span>clusterName<span class="token punctuation">,</span> brokerName<span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">HashMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>brokerAddrTable<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>brokerName<span class="token punctuation">,</span> brokerData<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
                <span class="token class-name">Map</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> brokerAddrsMap <span class="token operator">=</span> brokerData<span class="token punctuation">.</span><span class="token function">getBrokerAddrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//Switch slave to master: first remove &lt;1, IP:PORT&gt; in namesrv, then add &lt;0, IP:PORT&gt;</span>
                <span class="token comment">//The same IP:PORT must only have one record in brokerAddrTable</span>
                <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> brokerAddrsMap<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> item <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">!=</span> brokerAddr <span class="token operator">&amp;&amp;</span> brokerAddr<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>item<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> brokerId <span class="token operator">!=</span> item<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment">//不是第一次注册，即发送的是后续的心跳包的时候，直接替换原来的broker的信息</span>
                <span class="token class-name">String</span> oldAddr <span class="token operator">=</span> brokerData<span class="token punctuation">.</span><span class="token function">getBrokerAddrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>brokerId<span class="token punctuation">,</span> brokerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                registerFirst <span class="token operator">=</span> registerFirst <span class="token operator">||</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> oldAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>

                <span class="token comment">//topicQueueTable的维护</span>
                <span class="token comment">//如果broker是主节点</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">!=</span> topicConfigWrapper
                    <span class="token operator">&amp;&amp;</span> <span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token constant">MASTER_ID</span> <span class="token operator">==</span> brokerId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token comment">//Broker的topic配置信息发⽣变化或者是初次注册</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">isBrokerTopicConfigChanged</span><span class="token punctuation">(</span>brokerAddr<span class="token punctuation">,</span> topicConfigWrapper<span class="token punctuation">.</span><span class="token function">getDataVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
                        <span class="token operator">||</span> registerFirst<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">ConcurrentMap</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">TopicConfig</span><span class="token punctuation">&gt;</span></span> tcTable <span class="token operator">=</span>
                            topicConfigWrapper<span class="token punctuation">.</span><span class="token function">getTopicConfigTable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>tcTable <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token class-name">Map<span class="token punctuation">.</span>Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">TopicConfig</span><span class="token punctuation">&gt;</span></span> entry <span class="token operator">:</span> tcTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                <span class="token comment">//则需要创建或更新topic路由元数据，并填充topicQueueTable，其实就是为默认主题⾃动注册路由信息</span>
                                <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">createAndUpdateQueueData</span><span class="token punctuation">(</span>brokerName<span class="token punctuation">,</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token punctuation">}</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment">//更新BrokerLiveInfo，存储状态正常的Broker信息表，BrokeLiveInfo是执⾏路由删除操作的重要依据</span>
                <span class="token class-name">BrokerLiveInfo</span> prevBrokerLiveInfo <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>brokerAddr<span class="token punctuation">,</span>
                    <span class="token keyword">new</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">(</span>
                        <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                        topicConfigWrapper<span class="token punctuation">.</span><span class="token function">getDataVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span>
                        channel<span class="token punctuation">,</span>
                        haServerAddr<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> prevBrokerLiveInfo<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;new broker registered, {} HAServer: {}&quot;</span><span class="token punctuation">,</span> brokerAddr<span class="token punctuation">,</span> haServerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>

                <span class="token comment">//注册Broker的过滤器Server地址列表，⼀个Broker上会关联多个FilterServer消息过滤服务器</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>filterServerList <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>filterServerList<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token keyword">this</span><span class="token punctuation">.</span>filterServerTable<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>brokerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                        <span class="token keyword">this</span><span class="token punctuation">.</span>filterServerTable<span class="token punctuation">.</span><span class="token function">put</span><span class="token punctuation">(</span>brokerAddr<span class="token punctuation">,</span> filterServerList<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
                <span class="token comment">// 如果此Broker为从节点，则需要查找该Broker的主节点信息，并更新对应的masterAddr属性</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token constant">MASTER_ID</span> <span class="token operator">!=</span> brokerId<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token class-name">String</span> masterAddr <span class="token operator">=</span> brokerData<span class="token punctuation">.</span><span class="token function">getBrokerAddrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token class-name">MixAll</span><span class="token punctuation">.</span><span class="token constant">MASTER_ID</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>masterAddr <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">BrokerLiveInfo</span> brokerLiveInfo <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span>masterAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerLiveInfo <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            result<span class="token punctuation">.</span><span class="token function">setHaServerAddr</span><span class="token punctuation">(</span>brokerLiveInfo<span class="token punctuation">.</span><span class="token function">getHaServerAddr</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            result<span class="token punctuation">.</span><span class="token function">setMasterAddr</span><span class="token punctuation">(</span>masterAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&quot;registerBroker Exception&quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">return</span> result<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="设计亮点" tabindex="-1"><a class="header-anchor" href="#设计亮点" aria-hidden="true">#</a> 设计亮点</h3><p>NameServer与Broker保持长连接，Broker的状态信息存储在brokerLiveTable中，NameServer每收到⼀个心跳包，将更新brokerLiveTable中关于Broker的状态信息以及路由表（topicQueueTable、brokerAddrTable、brokerLiveTable、filterServerTable）。更新上述路由表（HashTable）使⽤了锁粒度较小的读写锁，允许多个消息发送者并发读操作，保证消息发送时的高并发。同⼀时刻NameServer只处理⼀个Broker心跳包，多个心跳包请求串行执行</p><h2 id="路由删除" tabindex="-1"><a class="header-anchor" href="#路由删除" aria-hidden="true">#</a> 路由删除</h2><p>上面谈到了如何发送心跳，但是如果broker宕机了，NameServer无法收到心跳包，NameServer如何删除无效的broker呢？</p><h3 id="定时扫描删除" tabindex="-1"><a class="header-anchor" href="#定时扫描删除" aria-hidden="true">#</a> 定时扫描删除</h3><p>在NameServer的初始化阶段，会启动一个定时任务，每隔10s扫描<code>brokerLiveTable（broker状态表）</code>中的broker，如果超过120s没有发送心跳包，就会将broker从<code>brokerLiveTable</code>中删除</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">scanNotActiveBroker</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">//遍历broker状态表，看看有没有要移除的broker</span>
        <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span></span> next <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token keyword">long</span> last <span class="token operator">=</span> next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getLastUpdateTimestamp</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">//比较当前时间与上一次时间，如果超过120s就认为broker不可用，从brokerLiveTable中移除</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token punctuation">(</span>last <span class="token operator">+</span> <span class="token constant">BROKER_CHANNEL_EXPIRED_TIME</span><span class="token punctuation">)</span> <span class="token operator">&lt;</span> <span class="token class-name">System</span><span class="token punctuation">.</span><span class="token function">currentTimeMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token class-name">RemotingUtil</span><span class="token punctuation">.</span><span class="token function">closeChannel</span><span class="token punctuation">(</span>next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                log<span class="token punctuation">.</span><span class="token function">warn</span><span class="token punctuation">(</span><span class="token string">&quot;The broker channel expired, {} {}ms&quot;</span><span class="token punctuation">,</span> next<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> <span class="token constant">BROKER_CHANNEL_EXPIRED_TIME</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//维护路由表以及其他信息（具体步骤）</span>
                <span class="token keyword">this</span><span class="token punctuation">.</span><span class="token function">onChannelDestroy</span><span class="token punctuation">(</span>next<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">,</span> next<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>具体的步骤代码如下：</p><div class="language-java line-numbers-mode" data-ext="java"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">onChannelDestroy</span><span class="token punctuation">(</span><span class="token class-name">String</span> remoteAddr<span class="token punctuation">,</span> <span class="token class-name">Channel</span> channel<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> brokerAddrFound <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>channel <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token comment">//加锁</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">readLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lockInterruptibly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> itBrokerLiveTable <span class="token operator">=</span>
                        <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment">//遍历，从broker状态表中找到要移除的broker的地址</span>
                    <span class="token keyword">while</span> <span class="token punctuation">(</span>itBrokerLiveTable<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerLiveInfo</span><span class="token punctuation">&gt;</span></span> entry <span class="token operator">=</span> itBrokerLiveTable<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">==</span> channel<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            brokerAddrFound <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">break</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token comment">//释放锁</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">readLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&quot;onChannelDestroy Exception&quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> brokerAddrFound<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            brokerAddrFound <span class="token operator">=</span> remoteAddr<span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;the broker&#39;s channel destroyed, {}, clean it&#39;s data structure at once&quot;</span><span class="token punctuation">,</span> brokerAddrFound<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
        <span class="token comment">//上面的代码就是找到要移除的broker的地址，重点在这里</span>
        <span class="token comment">//有要移除的broker</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerAddrFound <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> brokerAddrFound<span class="token punctuation">.</span><span class="token function">length</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&gt;</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

            <span class="token keyword">try</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    <span class="token comment">//加锁</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">lockInterruptibly</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment">//从broker状态表中移除该broker相关信息</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>brokerLiveTable<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>brokerAddrFound<span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment">//从filterServerTable中移除该broker相关信息</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>filterServerTable<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>brokerAddrFound<span class="token punctuation">)</span><span class="token punctuation">;</span>

                    <span class="token comment">//维护主从broker地址表</span>
                    <span class="token class-name">String</span> brokerNameFound <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
                    <span class="token keyword">boolean</span> removeBrokerName <span class="token operator">=</span> <span class="token boolean">false</span><span class="token punctuation">;</span>
                    <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">BrokerData</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> itBrokerAddrTable <span class="token operator">=</span>
                        <span class="token keyword">this</span><span class="token punctuation">.</span>brokerAddrTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token comment">//遍历主从broker地址表中的每一个主从架构的信息</span>
                    <span class="token keyword">while</span> <span class="token punctuation">(</span>itBrokerAddrTable<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">&amp;&amp;</span> <span class="token punctuation">(</span><span class="token keyword">null</span> <span class="token operator">==</span> brokerNameFound<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token comment">//每一个主从架构的broker的信息</span>
                        <span class="token class-name">BrokerData</span> brokerData <span class="token operator">=</span> itBrokerAddrTable<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                        <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> brokerData<span class="token punctuation">.</span><span class="token function">getBrokerAddrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token comment">//遍历当前的主从架构中所有的broker</span>
                        <span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Long</span><span class="token punctuation">,</span> <span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> entry <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">Long</span> brokerId <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">String</span> brokerAddr <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token comment">//找到要移除的broker，然后移除</span>
                            <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerAddr<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>brokerAddrFound<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                brokerNameFound <span class="token operator">=</span> brokerData<span class="token punctuation">.</span><span class="token function">getBrokerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove brokerAddr[{}, {}] from brokerAddrTable, because channel destroyed&quot;</span><span class="token punctuation">,</span>
                                    brokerId<span class="token punctuation">,</span> brokerAddr<span class="token punctuation">)</span><span class="token punctuation">;</span>
                                <span class="token keyword">break</span><span class="token punctuation">;</span>
                            <span class="token punctuation">}</span>
                        <span class="token punctuation">}</span>
                        <span class="token comment">//如果没有这个主从架构了，就说明需要移除主从broker地址表中的brokerName，因为brokerName相同的属于同一个主从架构</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerData<span class="token punctuation">.</span><span class="token function">getBrokerAddrs</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token comment">//标志位，表示是否移除了这个主从结构</span>
                            removeBrokerName <span class="token operator">=</span> <span class="token boolean">true</span><span class="token punctuation">;</span>
                            itBrokerAddrTable<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove brokerName[{}] from brokerAddrTable, because channel destroyed&quot;</span><span class="token punctuation">,</span>
                                brokerData<span class="token punctuation">.</span><span class="token function">getBrokerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment">//移除主从架构，也需要从集群地址表中移除这个主从架构</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerNameFound <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> removeBrokerName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Set</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> it <span class="token operator">=</span> <span class="token keyword">this</span><span class="token punctuation">.</span>clusterAddrTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">while</span> <span class="token punctuation">(</span>it<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">Set</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> entry <span class="token operator">=</span> it<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">String</span> clusterName <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">Set</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">&gt;</span></span> brokerNames <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">boolean</span> removed <span class="token operator">=</span> brokerNames<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span>brokerNameFound<span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">if</span> <span class="token punctuation">(</span>removed<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove brokerName[{}], clusterName[{}] from clusterAddrTable, because channel destroyed&quot;</span><span class="token punctuation">,</span>
                                    brokerNameFound<span class="token punctuation">,</span> clusterName<span class="token punctuation">)</span><span class="token punctuation">;</span>
                                <span class="token comment">//如果移除了这个主从架构之后，集群为空了，那么也需要移除这个集群</span>
                                <span class="token keyword">if</span> <span class="token punctuation">(</span>brokerNames<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove the clusterName[{}] from clusterAddrTable, because channel destroyed and no broker in this cluster&quot;</span><span class="token punctuation">,</span>
                                        clusterName<span class="token punctuation">)</span><span class="token punctuation">;</span>
                                    it<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                <span class="token punctuation">}</span>

                                <span class="token keyword">break</span><span class="token punctuation">;</span>
                            <span class="token punctuation">}</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                    <span class="token comment">//如果移除了主从结构，根据BrokerName，遍历所有主题的队列，如果队列中</span>
                    <span class="token comment">//包含当前Broker的队列，则移除，如果topic只包含待移除Broker的</span>
                    <span class="token comment">//队列，从路由表中删除该topic，</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>removeBrokerName<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Entry</span><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">QueueData</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> itTopicQueueTable <span class="token operator">=</span>
                            <span class="token keyword">this</span><span class="token punctuation">.</span>topicQueueTable<span class="token punctuation">.</span><span class="token function">entrySet</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">while</span> <span class="token punctuation">(</span>itTopicQueueTable<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            <span class="token class-name">Entry</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">String</span><span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token punctuation">&lt;</span><span class="token class-name">QueueData</span><span class="token punctuation">&gt;</span><span class="token punctuation">&gt;</span></span> entry <span class="token operator">=</span> itTopicQueueTable<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">String</span> topic <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getKey</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">QueueData</span><span class="token punctuation">&gt;</span></span> queueDataList <span class="token operator">=</span> entry<span class="token punctuation">.</span><span class="token function">getValue</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

                            <span class="token class-name">Iterator</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">QueueData</span><span class="token punctuation">&gt;</span></span> itQueueData <span class="token operator">=</span> queueDataList<span class="token punctuation">.</span><span class="token function">iterator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token keyword">while</span> <span class="token punctuation">(</span>itQueueData<span class="token punctuation">.</span><span class="token function">hasNext</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                <span class="token class-name">QueueData</span> queueData <span class="token operator">=</span> itQueueData<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                <span class="token keyword">if</span> <span class="token punctuation">(</span>queueData<span class="token punctuation">.</span><span class="token function">getBrokerName</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span>brokerNameFound<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                    itQueueData<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                    log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove topic[{} {}], from topicQueueTable, because channel destroyed&quot;</span><span class="token punctuation">,</span>
                                        topic<span class="token punctuation">,</span> queueData<span class="token punctuation">)</span><span class="token punctuation">;</span>
                                <span class="token punctuation">}</span>
                            <span class="token punctuation">}</span>

                            <span class="token keyword">if</span> <span class="token punctuation">(</span>queueDataList<span class="token punctuation">.</span><span class="token function">isEmpty</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                                itTopicQueueTable<span class="token punctuation">.</span><span class="token function">remove</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                                log<span class="token punctuation">.</span><span class="token function">info</span><span class="token punctuation">(</span><span class="token string">&quot;remove topic[{}] all queue, from topicQueueTable, because channel destroyed&quot;</span><span class="token punctuation">,</span>
                                    topic<span class="token punctuation">)</span><span class="token punctuation">;</span>
                            <span class="token punctuation">}</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
                    <span class="token comment">//释放锁</span>
                    <span class="token keyword">this</span><span class="token punctuation">.</span>lock<span class="token punctuation">.</span><span class="token function">writeLock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">unlock</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                log<span class="token punctuation">.</span><span class="token function">error</span><span class="token punctuation">(</span><span class="token string">&quot;onChannelDestroy Exception&quot;</span><span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="broker正常关闭删除" tabindex="-1"><a class="header-anchor" href="#broker正常关闭删除" aria-hidden="true">#</a> broker正常关闭删除</h3><p>broker正常关闭的删除逻辑和定时扫描删除调用的方法是一样的，只不过，broker正常关闭时会调用会执行<code>unregisterBroker</code>指令</p></div><!--[--><!----><!--]--><footer class="page-meta"><div class="meta-item edit-link"><a href="https://github.com/eddie-lucas/edit/main/src/backend/rocketmq/NameServer.md" rel="noopener noreferrer" target="_blank" aria-label="编辑此页" class="nav-link label"><!--[--><svg xmlns="http://www.w3.org/2000/svg" class="icon edit-icon" viewBox="0 0 1024 1024" fill="currentColor" aria-label="edit icon"><path d="M430.818 653.65a60.46 60.46 0 0 1-50.96-93.281l71.69-114.012 7.773-10.365L816.038 80.138A60.46 60.46 0 0 1 859.225 62a60.46 60.46 0 0 1 43.186 18.138l43.186 43.186a60.46 60.46 0 0 1 0 86.373L588.879 565.55l-8.637 8.637-117.466 68.234a60.46 60.46 0 0 1-31.958 11.229z"></path><path d="M728.802 962H252.891A190.883 190.883 0 0 1 62.008 771.98V296.934a190.883 190.883 0 0 1 190.883-192.61h267.754a60.46 60.46 0 0 1 0 120.92H252.891a69.962 69.962 0 0 0-69.098 69.099V771.98a69.962 69.962 0 0 0 69.098 69.098h475.911A69.962 69.962 0 0 0 797.9 771.98V503.363a60.46 60.46 0 1 1 120.922 0V771.98A190.883 190.883 0 0 1 728.802 962z"></path></svg><!--]-->编辑此页<span><svg class="external-link-icon" xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path><polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg><span class="external-link-icon-sr-only">open in new window</span></span><!----></a></div><div class="meta-item git-info"><div class="update-time"><span class="label">上次编辑于: </span><!----></div><div class="contributors"><span class="label">贡献者: </span><!--[--><!--[--><span class="contributor" title="email: 761404955@qq.com">Lucas</span><!--]--><!--]--></div></div></footer><nav class="vp-page-nav"><!----><a aria-label="消息发送" class="vp-link nav-link next nav-link next" href="/backend/rocketmq/%E6%B6%88%E6%81%AF%E5%8F%91%E9%80%81.html"><div class="hint">下一页<span class="arrow end"></span></div><div class="link">消息发送<!----></div></a></nav><div id="comment" class="giscus-wrapper input-top" style="display:none;"><div class="loading-icon-wrapper" style="display:flex;align-items:center;justify-content:center;height:96px"><svg xmlns="http://www.w3.org/2000/svg" width="48" height="48" preserveAspectRatio="xMidYMid" viewBox="25 25 50 50"><animateTransform attributeName="transform" type="rotate" dur="2s" keyTimes="0;1" repeatCount="indefinite" values="0;360"></animateTransform><circle cx="50" cy="50" r="20" fill="none" stroke="currentColor" stroke-width="4" stroke-linecap="round"><animate attributeName="stroke-dasharray" dur="1.5s" keyTimes="0;0.5;1" repeatCount="indefinite" values="1,200;90,200;1,200"></animate><animate attributeName="stroke-dashoffset" dur="1.5s" keyTimes="0;0.5;1" repeatCount="indefinite" values="0;-35px;-125px"></animate></circle></svg></div></div><!--[--><!----><!--]--><!--]--></main><!--]--><footer class="vp-footer-wrapper"><div class="vp-footer">记录，分享自己的学习过程.....</div><div class="vp-copyright">Copyright © 2025 小刘Learning</div></footer></div><!--]--><!--]--><!----><!----><!--]--></div>
    <script type="module" src="/assets/app-d83e1369.js" defer></script>
  </body>
</html>
